package com.hongkuncheng.cms.freemaker.article;

import java.util.List;

import org.springframework.stereotype.Component;
import com.hongkuncheng.cms.helper.ArticleHelper;
import com.hongkuncheng.cms.model.Article;
import com.hongkuncheng.cms.model.Columns;
import com.hongkuncheng.cms.model.Zhuanti;
import freemarker.ext.beans.BeanModel;
import freemarker.ext.beans.StringModel;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

@Component
public class GetDetailLinkMethod implements TemplateMethodModelEx {

    @Override
    public Object exec(List args) throws TemplateModelException {
        BeanModel mo = (BeanModel) args.get(0);
        Article article = (Article) mo.getAdaptedObject(Article.class);
        String result = null;
        switch (args.size()) {
            case 1:
                result = ArticleHelper.getDetailLink(article);
                break;
            case 2:
                if (args.get(1).getClass().equals(SimpleScalar.class)) {
                    result = ArticleHelper.getDetailLink(article, args.get(1).toString());
                } else if (args.get(1).getClass().equals(StringModel.class)) {
                    BeanModel bmColumns = (BeanModel) args.get(1);
                    result = ArticleHelper.getDetailLink(article, (Columns) bmColumns.getAdaptedObject(Columns.class));
                }
                break;
            case 3:
                BeanModel bmZhuanti = (BeanModel) args.get(1);
                result = ArticleHelper.getDetailLink(article, (Zhuanti) bmZhuanti.getAdaptedObject(Zhuanti.class),
                        args.get(2).toString());
                break;
        }
        return result;
    }

}
